08. 1D 向量练习区

练习区

现在,轮到你进行向量编程了。在这个练习区里,你可以编写自己的程序。

想法 1

首先,尝试编写一个初始化大小为 3 的向量的程序。这个向量的值是 [5, 10, 27] 。使用值 [3, 17, 12] 初始化另一个大小为 3 的向量。现在将这两个相量相减并输出结果。

要想多加练习,你可以写一个函数,接受两个向量,然后输出向量之间的差。假设两个向量大小相同;否则你必须检查并确定它们大小相同,同时还有进行错误检查。

想法 2

使用值 [17, 10, 31, 5, 7] 初始化一个向量。使用值 [3, 1, 6, 19, 8] 初始化另一个向量。然后,输出另一个包含每个元素的乘积的向量。换句话说,向量应该有 [17\times3, \space10\times1, \space 31\times6, \space 5\times19, \space 7\times8]

要想多加练习,你可以写一个函数,接受两个向量,然后输出一个元素逐个相乘得到的新向量。假设两个向量大小相同;否则你必须检查并确定它们大小相同,同时还有进行错误检查。

Start Quiz:

//TODO: Use this as a playground to practice with vectors


//TODO:
// Fill out your program's header. The header should contain any necessary
// include statements and also function declarations


//TODO:
// Write your main program. Remember that all C++ programs need
// a main function. The most important part of your program goes
// inside the main function. 
#include <iostream>
#include <vector>

using namespace std;

// function declaration
vector<float> vectorsubtraction(vector<float> vector1, vector<float> vector2);

// program that computes the difference between two vectors
int main() {

	// declare and initialize vectors
	vector<float> v1(3);
	vector<float> v2(3);
	
	v1[0] = 5.0;
	v1[1] = 10.0;
	v1[2] = 27.0;
	
	v2[0] = 2.0;
	v2[1] = 17.0;
	v2[2] = 12.0;
	
	vector<float> v3 (v1.size());

	// calculate the difference between the two vectors
	v3 = vectorsubtraction(v1, v2);

	// print out the results of the vector subtraction
	for (int i = 0; i < v3.size(); i++) {
		cout << v3[i] << " ";
	}

	cout << endl;
	
	return 0;

}

// define the function - 
// INPUTS: two vectors
// OUTPUT: the difference between the two vectors
vector<float> vectorsubtraction(vector<float> vector1, vector<float> vector2) {

	vector<float> vectordifference (vector1.size());

	for (int i = 0; i < vector1.size(); i++) {
		vectordifference[i] = vector1[i] - vector2[i];
	}

	return vectordifference;

}
#include <iostream>
#include <vector>

using namespace std;

// function declaration
vector<float> vectormultiply(vector<float> vector1, vector<float> vector2);

// program that computes the element-wise multiplication of two vectors
int main() {

	// declare and initialize vectors
	vector<float> v1(5);
	vector<float> v2(5);
	
	v1[0] = 17.0;
	v1[1] = 10.0;
	v1[2] = 31.0;
	v1[3] = 5.0;
	v1[4] = 7.0;

	v2[0] = 3.0;
	v2[1] = 1.0;
	v2[2] = 6.0;
	v2[3] = 19.0;
	v2[4] = 8.0;

	vector<float> v3 (v1.size());

	// calculate the difference between the two vectors
	v3 = vectormultiply(v1, v2);

	// print out the results of the vector multiplication
	for (int i = 0; i < v3.size(); i++) {
		cout << v3[i] << " ";
	}

	cout << endl;
	
	return 0;

}

// define the function - 
// INPUTS: two vectors
// OUTPUT: multiplies elements together into a new vector
vector<float> vectormultiply(vector<float> vector1, vector<float> vector2) {

	vector<float> vectorproduct (vector1.size());

	for (int i = 0; i < vector1.size(); i++) {
		vectorproduct[i] = vector1[i] * vector2[i];
	}

	return vectorproduct;

}